let token = sessionStorage.getItem("token");
$(function () {

    loadData(1);


});
//添加团
$("#go").click(function () {
    let group_name = $("#group_name").val();
    let  tuan_id = $("#tuan").val();
    $.ajax({
        url: testurl + "deputy/team/",
        type: "post",
        headers: {"Authorization": "Bearer " + token},
        contentType: "application/json",
        data: JSON.stringify({
            "type":"1",
            "name":group_name
        }),
        success:function (data) {
            if (data.success === true) {
                $('#my-popup').modal('close');
                $(".users").html("添加委员团组");
                $(".alert").html(data.message);
                $('#my-alert').modal({target: '#my-alert'});
                loadData(1);
            }else {
                $('#my-popup').modal('close');
                $(".users").html("添加委员团组");
                $(".alert").html(data.message);
                $('#my-alert').modal({target: '#my-alert'});
                loadData(1);
            }
        }
    });


    return false
});


//页数
let pages = 1;
function loadData(pageNum)  {
    $.ajax({
        url: testurl + "deputy/team/list",
        type: "post",
        headers: {"Authorization": "Bearer " + token},
        contentType: "application/json",
        data: JSON.stringify({
            "type": "1",
            "pageNum": pageNum
        }),
        success: function (data) {
            let groupmessage = data.obj;
            ssd = Math.ceil(data.pageTotal / data.pageSize);
            pages = ssd;
            str = "";
            $.each(groupmessage, function (index, values) {
                str += " <tr style='cursor: pointer' onclick='getDuemeg("+values.id+")' class=\"gradeX\">\n" +
                    "                                        <td>" + values.name + "</td>\n" +
                    "                                        <td>" + values.db_num + "</td>\n" +
                    " </tr>";
            });
            $("#show").html(str);
        }
    });
}
//分页
let pageNum = 1;
$(".first").click(function () {
    pageNum = 1;
    loadData(pageNum);
    $(this).parents("li").addClass("am-active");
    $(this).parents("li").siblings().removeClass("am-active");
});
$(".last").click(function () {
    pageNum = pages;
    loadData(pageNum);
    $(this).parents("li").addClass("am-active");
    $(this).parents("li").siblings().removeClass("am-active");
});
$(".up").click(function () {
    if (pageNum === 1) {
        return false;
    } else {
        pageNum--;
        loadData(pageNum);
    }
    $(this).parents("li").addClass("am-active");
    $(this).parents("li").siblings().removeClass("am-active");
});
$(".down").click(function () {
    if (pageNum === pages) {
        return false;
    } else {
        pageNum++;
        loadData(pageNum);
    }
    $(this).parents("li").addClass("am-active");
    $(this).parents("li").siblings().removeClass("am-active");
});
// 得到委员团组详细信息
function getDuemeg(e) {
    $.ajax({
        url: testurl + "deputy/team/detail",
        type: "post",
        headers: {"Authorization": "Bearer " + token},
        contentType: "application/json",
        data: JSON.stringify({
            "id":e
        }),
        success:function (data) {
            $("#groupname").val(data.obj.name);
            $("#group_id").val(data.obj.id);
            $("#tuann").val(data.obj.pid);
        }
    })
}

//保存修改委员团组详细信息
$("#save").click(function () {
    let group_id = $("#group_id").val();
    let groupname = $("#groupname").val();
    if (groupname === "" || groupname === ""){
        $(".users").html("保存委员团组");
        $(".alert").html("请选择要修改的委员团组");
        $('#my-alert').modal({target: '#my-alert'});
    } else{
        $.ajax({
            url: testurl + "deputy/team/update",
            type: "post",
            headers: {"Authorization": "Bearer " + token},
            contentType: "application/json",
            data: JSON.stringify({
                "id":group_id,
                "type":"1",
                "name":groupname
            }),
            success:function (data) {
                if (data.success === true) {
                    $(".users").html("修改委员团组");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    loadData(1);
                }else {
                    $(".users").html("修改委员团组");
                    $(".alert").html(data.message);
                    $('#my-alert').modal({target: '#my-alert'});
                    loadData(1);
                }
            }
        });
    }

    return false
});
//删除委员团组
$("#delete").click(function () {
    let group_id = $("#group_id").val();
    if (group_id === ""){
        $(".users").html("删除委员团组");
        $(".alert").html("请选择要删除的委员团组");
        $('#my-alert').modal({target: '#my-alert'});
    } else{
        if(confirm("确定删除所选委员团组？")){
            $.ajax({
                url: testurl + "deputy/team/deletes",
                type: "post",
                headers: { "Authorization": "Bearer " + token },
                contentType: "application/json",
                data: JSON.stringify({
                    "ids": [group_id]
                }),
                success: function (data) {
                    if (data.success === true) {
                        $("#groupname").val("");
                        $(".users").html("删除委员团组");
                        $(".alert").html(data.message);
                        $('#my-alert').modal({ target: '#my-alert' });
                        loadData(1);
                    } else {
                        $(".users").html("删除委员团组");
                        $(".alert").html(data.message);
                        $('#my-alert').modal({ target: '#my-alert' });
                        loadData(1);
                    }
                }
            });
        }
  
    }
    return false
});